import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Layout from '@/layout/index.vue'
import HomeRoute from './modules/home'
import store from '@/store/'

Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName:'login' */'@/views/login/index.vue')
  },
  {
    path: '/',
    component: Layout,
    meta: {
      requestToken: true
    },
    children: [
      ...HomeRoute
    ]
  },
  {
    path: '*',
    name: '404',
    component: () => import(/* webpackChunkName:'404' */'@/views/error-page/404.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(item => item.meta.requestToken)) {
    if (!store.state.userToken) {
      next({
        name: 'login',
        query: {
          redirect: to.fullPath
        }
      })
    } else next()
  } else next()
})

export default router

/**
 * '/' home
 *
 * '/login' 登录管理
 *
 * 权限管理
 * '/role' 角色管理
 * '/menu' 菜单管理
 * '/resourse' 资源管理
 *
 * '/course' 课程管理
 *
 * '/user' 用户管理
 *
 * 广告管理
 * '/advert' 广告位
 * '/advert-space' 广告位管理
 * */
